﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

using BeholdTV.Sample.Properties;

namespace BeholdTV.Sample
{
    public partial class MainForm : Form
    {
        private const int MaxLogLines = 500;

        private readonly RemoteControl brc;
        private readonly List<string> log = new List<string>(MaxLogLines);

        public MainForm()
        {
            InitializeComponent();

            brc = new RemoteControl();
            brc.ButtonPressed += RemoteControlButtonPressed;

            cbCards.Items.Clear();
            cbCards.Items.AddRange(brc.Cards.ToArray());
            cbCards.Enabled = cbCards.Items.Count > 0;
            if (!cbCards.Enabled)
                cbCards.Text = Resources.TextCardsIsNotInstalled;

            textBox1.Clear();
            textBox2.Clear();
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            brc.Dispose();
        }

        private delegate void ButtonPressedDelegate(uint code, uint codeEx);

        private void RemoteControlButtonPressed(object sender, RemoteControlButtonEventArgs e)
        {
            ButtonPressedDelegate x = (code, codeEx) =>
            {
                textBox1.Text = String.Format("0x{0:X2} ({0}) [0x{1:X8}]", code, codeEx);
                
				if (log.Count >= MaxLogLines)
					log.RemoveRange(0, MaxLogLines - log.Count + 1);

				log.Add(String.Format("{0:hh:mm:ss} 0x{1:X8} [0x{2:X8}]", DateTime.Now, code, codeEx));
                
				textBox2.Lines = log.ToArray();
                textBox2.SelectionStart = textBox2.Text.Length;
            };

            Invoke(x, e.Code, e.CodeEx);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "Start")
                StartPolling();
            else
                StopPolling();
        }

        private void StopPolling()
        {
            brc.StopPolling();
            button1.Text = "Start";
        }

        private void StartPolling()
        {
            brc.StartPolling(cbCards.SelectedIndex);
            button1.Text = "Stop";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox2.Clear();
        }

        private void cbCards_SelectedIndexChanged(object sender, EventArgs e)
        {
            brc.InitializeCard(cbCards.SelectedIndex);

            LoadImage();

            button1.Enabled = true;
        }

        private void LoadImage()
        {
			//Bitmap image;

			//switch (brc.RemoteControlType)
			//{
			//    case RemoteControlType.Buttons34:
			//        image = Resources.Buttons34;
			//        break;

			//    case RemoteControlType.Buttons28:
			//        image = Resources.Buttons28;
			//        break;

			//    case RemoteControlType.Buttons30:
			//        image = Resources.Buttons30;
			//        break;

			//    default:
			//        image = Resources.Unknown;
			//        break;
			//}

			//pictureBox1.Image = image;

			pictureBox1.BackgroundImage = Resources.ResourceManager.GetObject(brc.RemoteControlType.ToString()) as Bitmap;
        }
    }
}
